#pragma once 
#include "ReadBDF.h"
#include "GeneralMethod.h"

#include <Python.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkCellArray.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkHexahedron.h>

#define GETI(value) *get_if<int>(&value)
#define GETF(value) *get_if<float>(&value)
#define GETS(value) *get_if<std::string>(&value)

//ReadBDF_::ReadBDF_()
//{
//	set<string> temp_set;
//	temp_set.insert(nastran_type.begin(), nastran_type.end());
//	temp_set.insert(nodes_related_keywords.begin(), nodes_related_keywords.end());
//	temp_set.insert(coord_related_keywords.begin(), coord_related_keywords.end());
//	all_keywords.assign(temp_set.begin(), temp_set.end());
//}

ReadBDF_* ReadBDF_::_instance = nullptr;

void ReadBDF_::read_bdf_data() {
	clock_t start, end;
	start = clock();
	read_all_keywords();

	end = clock();
	cout << "Finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";

	//对得到的read_all_keywords进行拆分
	vector<vector<string>> nodes_packages = deal_all_packages_out[0];
	vector<vector<string>> elements_packages = deal_all_packages_out[1];

	//自定义集合
	start = clock();
	cout << "\nStart dealing with users set...\n";
	read_users_set();
	end = clock();
	cout << "Finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";

	// 节点信息
	start = clock();
	cout << "\nStart dealing with nodes...\n";
	read_nodes();
	end = clock();
	cout << "Finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";

	// 单元信息
	start = clock();
	cout << "\nStart dealing with elements...\n";
	read_elements();
	end = clock();
	cout << "Finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";



	//class SetsDict sets_dict_result;
	//record_sets_dict(read_displacement_result, read_elements_result, sets_dict_result, read_rbe_result, read_nodes_result);

	//result.users_sets = set_msg_map;
	//result.elements_names = read_elements_result.elements_names;
	//result.elements_points = read_elements_result.elements_points;
	//result.elements_sequence = read_elements_result.elements_sequence;
	//result.nodes_coord = nodes_coord;
	//result.nodes_sequence = nodes_sequence;
	//result.displacement = read_displacement_result.all_case_displacement;
	//result.c_force = read_displacement_result.all_case_c_force;
	//result.moment = read_displacement_result.all_case_moment;
	//result.gravity = read_displacement_result.all_case_gravity;
	////result.pload = read_displacement_result.all_case_pload;
	//result.pload1 = read_displacement_result.all_case_pload1;
	//result.pload4 = read_displacement_result.all_case_pload4;
	//result.pressure = read_displacement_result.all_case_pressure;
	//result.mpc = read_displacement_result.all_mpc;
	//result.time_response = read_displacement_result.all_case_time_response;
	//result.material = read_material_result.property;
	//// 材料中的table暂时不用，在read_table中解析
	////result.table = read_material_result.table;
	//result.rbe = read_rbe_result;
	//result.contact = read_contact_result;
	//result.temperature = read_temperature_result;
	//result.table = all_table_result;
	//result.sets_dict = sets_dict_result;
	//result.extra.property_msg = read_elements_result.property_msg_new;
	//result.extra.solve_msg = read_solver_result;
	//result.extra.surfaces_msg = element_surfaces;
	//result.extra.nodes_mapping_dict = nodes_mapping_dict;
	//result.extra.ele_mapping_dict = read_elements_result.ele_mapping_dict;
	//result.extra.param = read_param_result;
	//result.extra.cgap = read_elements_result.gap_msg;
	//result.extra.coordinate = read_displacement_result.all_coordinate;
	//result.extra.bush = read_elements_result.bush_msg;
	//result.extra.suport = read_displacement_result.all_case_suport;
	//result.extra.celas = read_elements_result.celas_msg;
	//result.extra.case_msg = read_displacement_result.case_msg;
	//result.extra.output_msg = all_output_data;
	//result.extra.not_recording_keywords = not_recording_keywords;

	//for (auto& i : read_solver_result.solver_type) {
	//	if (i.second.SOL != 0) {
	//		result.extra.SOL = i.second.SOL;
	//		break;
	//	}
	//}
};

int ReadBDF_::write_gmsh_file()
{
	Py_Initialize();
	//PyRun_SimpleString("import meshio");
	PyObject* sys = PyImport_ImportModule("sys");
	PyObject* path = PyObject_GetAttrString(sys, "path");
	string py_file_path = exe_path;
	PyList_Append(path, PyUnicode_FromString(py_file_path.c_str()));

	PyObject* pModule = PyImport_ImportModule("WriteBDF");
	if (!pModule)
	{
		std::cout << "something wrong!\n";
		return -1;
	}

	PyObject* function = PyObject_GetAttrString(pModule, "write_gmsh");
	if (!function || !PyCallable_Check(function))
	{
		PyErr_Print();
		std::cout << "something wrong!\n";
		return -1;
	}

	PyObject* nodes_coord_py = PyList_New(nodes_coord.size());
	for (int i = 0; i < nodes_coord.size(); i++)
	{
		vector<double> temp_data;
		for (float& v : (nodes_coord)[i])
		{
			temp_data.emplace_back(static_cast<double>(v));
		}
		PyObject* temp = GeneralMethod::change_vector_to_list(temp_data);
		PyList_SET_ITEM(nodes_coord_py, i, temp);
	}
	PyObject* elements_points_py = PyDict_New();
	elements_points_py = GeneralMethod::change_map_to_dict(elements_points);
	PyObject* elements_sequence_py = PyDict_New();
	std::map<std::string, std::vector<int>> temp_map;
	for (auto&i:elements_sequence)
	{
		temp_map[nastran_type_to_meshio_type[i.first]] = i.second;
	}
	elements_sequence_py = GeneralMethod::change_map_to_dict(temp_map);

	auto pos = read_file_path.find_last_of('/');
	string file_name = read_file_path.substr(pos+1);
	PyObject* file_name_py = PyUnicode_FromString(file_name.c_str());
	PyObject* write_file_path_py = PyUnicode_FromString(write_file_path.c_str());
	PyObject* res = PyObject_CallObject(function, PyTuple_Pack(5, nodes_coord_py, elements_points_py, elements_sequence_py, file_name_py, write_file_path_py));

	if (!res)
	{
		PyErr_Print();
	}
	
	return 1;
}

int ReadBDF_::write_vtk_file()
{
	auto setVTKCELLTYPE = [&](vtkIdType* ele_index,vector<int>& _index)
	{
		for (size_t i = 0; i < _index.size(); i++)
		{
			ele_index[i] = static_cast<VTKCellType>(_index[i]);
		}
	};

	std::map<string, VTKCellType> nastran_to_vtk_type =
	{
		std::make_pair("CONM2", VTK_VERTEX),
			std::make_pair("CELAS1", VTK_LINE),
			std::make_pair("CELAS2", VTK_LINE),
			std::make_pair("CBEAM", VTK_LINE),
			std::make_pair("CBUSH", VTK_LINE),
			std::make_pair("CBUSH1D", VTK_LINE),
			std::make_pair("CROD", VTK_LINE),
			std::make_pair("CGAP", VTK_LINE),
			std::make_pair("CBAR", VTK_LINE),
			std::make_pair("CTETRA", VTK_TETRA),
			std::make_pair("CPYRAM", VTK_EMPTY_CELL),
			std::make_pair("CPYRA", VTK_EMPTY_CELL),
			std::make_pair("CPENTA", VTK_WEDGE),
			std::make_pair("CHEXA", VTK_HEXAHEDRON),
			std::make_pair("CTRIAR", VTK_TRIANGLE),
			std::make_pair("CTRIA3", VTK_TRIANGLE),
			std::make_pair("CTRAX6", VTK_QUADRATIC_TRIANGLE),
			std::make_pair("CTRIAX6", VTK_QUADRATIC_TRIANGLE),
			std::make_pair("CTRIA6", VTK_QUADRATIC_TRIANGLE),
			std::make_pair("CQUADR", VTK_QUAD),
			std::make_pair("CSHEAR", VTK_EMPTY_CELL),
			std::make_pair("CQUAD4", VTK_QUAD),
			std::make_pair("CQUAD8", VTK_QUADRATIC_QUAD),
	};
	vtkNew<vtkPoints> all_points;
	for (auto&i: nodes_coord)
	{
		all_points->InsertNextPoint(i[0], i[1], i[2]);
	}
	vtkNew<vtkUnstructuredGrid> Grid;
	Grid->SetPoints(all_points);
	for (auto&i:elements_sequence)
	{
		VTKCellType cell_type = nastran_to_vtk_type[i.first];
		for (auto&j:i.second)
		{
			vtkIdType* ele_index = new vtkIdType[elements_points[j].size()];
			/*for (size_t k = 0; k < elements_points[j].size(); k++)
			{
				ele_index[k] = static_cast<VTKCellType>(elements_points[j][k]);
			}*/
			setVTKCELLTYPE(ele_index, elements_points[j]);
			Grid->InsertNextCell(cell_type, elements_points[j].size(), ele_index);
		}
	}
	vtkNew<vtkXMLUnstructuredGridWriter> writer;
	writer->SetInputData(Grid);
	auto pos = read_file_path.find_last_of('/');
	auto pos2 = read_file_path.find_last_of('.');
	string file_name = read_file_path.substr(pos + 1, pos2-pos-1);
	writer->SetFileName((write_file_path+"/"+ file_name+".vtu").c_str());
	writer->Write();
	return 1;
}

int ReadBDF_::write_gmsh_file_from_vtk() 
{
	/*string sys_path_str = exe_path;
	sys_path_str+=":";
	sys_path_str+=(exe_path+"/lib:");
	sys_path_str+=(exe_path+"/lib/python3.12/site-packages:");
	sys_path_str+=(exe_path+"/lib/python3.12/lib-dynload:");
	sys_path_str+=(exe_path+"/lib/python3.12");
	wchar_t* test = GeneralMethod::char_to_wchar(sys_path_str.c_str());

	Py_SetPath(test);*/
	Py_Initialize();
	std::vector<PyObject*> pyObjects;
	PyRun_SimpleString("import sys;");
	// PyObject* sys = PyImport_ImportModule("sys");
	// PyObject* path = PyObject_GetAttrString(sys, "path");
	// string py_file_path = exe_path;
	// PyList_Append(path, PyUnicode_FromString(py_file_path.c_str()));

	PyObject* pModule = PyImport_ImportModule("WriteBDF");
	pyObjects.emplace_back(pModule);
	if (!pModule)
	{
		std::cout << "something wrong!\n";
		return -1;
	}
	PyObject* function = PyObject_GetAttrString(pModule, "write_gmsh_from_vtk");
	pyObjects.emplace_back(function);
	if (!function || !PyCallable_Check(function))
	{
		PyErr_Print();
		std::cout << "something wrong!\n";
		return -1;
	}
	string file_name = read_file_path.substr(read_file_path.find_last_of("/") + 1);
	string file_name_without_suffix = file_name.substr(0,file_name.find_last_of("."));
	PyObject* vtk_path_py = PyUnicode_FromString((write_file_path+"/"+ file_name_without_suffix+".vtu").c_str());
	PyObject* write_file_path_py = PyUnicode_FromString((write_file_path+"/"+ file_name_without_suffix+".msh").c_str());
	pyObjects.emplace_back(vtk_path_py);
	pyObjects.emplace_back(write_file_path_py);
	PyObject* res = PyObject_CallObject(function, PyTuple_Pack(2, vtk_path_py, write_file_path_py));
	pyObjects.emplace_back(res);
	//cout << "test" << endl;
	for (auto it = pyObjects.rbegin(); it != pyObjects.rend(); it++)
	{
		Py_XDECREF(*it); // 释放python空间
		//cout << "test2" << endl;
	}
	std::vector<PyObject*>().swap(pyObjects);
	//cout << "test3" << endl;
	Py_Finalize();
	//cout << "test4" << endl;
	return 0;
};

void ReadBDF_::read_all_keywords() {
	clock_t sta, end;

	//在bdf中寻找所有关键词
	vector<string> all_packages;

	look_for_msg(all_packages);

	vector<vector<string>> deal_all_packages;
	split_and_combine(all_packages, deal_all_packages);

	//对所有的关键词进行切分
	sta = clock();
	vector<vector<string>> division_keywords = { nodes_related_keywords, nastran_type };

	division(deal_all_packages, division_keywords, deal_all_packages_out);
	end = clock();

};

void ReadBDF_::read_users_set() {
	vector<vector<string>> set_msg;
	string line;
	fstream file(read_file_path, ios::in);
	bool continue_control = false;//控制一个set是否存在多行
	if (!file.is_open()) {
		cout << "Warning: path " << read_file_path << " 打开文件失败\n";
		throw;
	}
	//寻找set
	while (getline(file, line)) {
		if (line.find('$') == 0) {
			continue;
			continue_control = false;
		}
		GeneralMethod::to_upper(line);
		GeneralMethod::strip(line);
		if (line.find("BEGIN BULK") != string::npos || line.find("GRID") != string::npos) {
			break;
		}

		if (line.find("SET") == 0) {
			set_msg.emplace_back();
			set_msg.back().push_back(line);
			continue_control = true;
		}
		else if ((isdigit(line[0]) || line[0] == ',') && continue_control == true) {
			set_msg.back().push_back(line);
		}
		else if (isalpha(line[0])) {
			continue_control = false;
		}
	}

	//提取set名称
	vector<int> set_id;
	for (auto& i : set_msg) {
		string temp = i[0].substr(0, i[0].find("="));
		temp = temp.erase(0, 3);
		if (temp.empty()) {
			cout << "Warning: 存在名称为空的集合，请检查文件\n";
		}
		else {
			set_id.push_back(stoi(temp));
			i[0] = i[0].substr(i[0].find("=") + 1);
		}
	}

	//每个set的节点进行拆分
	vector<vector<string>> deal_set_msg;
	char split_ = ',';
	for (auto& i : set_msg) {
		deal_set_msg.emplace_back();
		for (auto& j : i) {
			string data_;
			stringstream ss(j);
			while (getline(ss, data_, split_)) {
				deal_set_msg.back().push_back(data_);
			}
		}
	}

	for (int i = 0; i < deal_set_msg.size(); i++) {
		set_msg_map[set_id[i]] = {};
		for (auto& j : deal_set_msg[i]) {
			if (j.find(".") != string::npos) {
				cout << "Warning: 暂无法解析包含浮点数的SET集合，请检查代码\n";
				set_msg_map[set_id[i]].clear();
				break;
			}
			else if (j.find("EXCEPT") != string::npos) {
				cout << "Warning: 暂无法解析包含EXCEPT的SET集合，请检查代码\n";
				set_msg_map[set_id[i]].clear();
				break;
			}
			else if (j.find("THRU") != string::npos) {
				int num1 = stoi(j.substr(0, j.find("T")));
				int num2 = stoi(j.substr(j.find("U") + 1));
				vector<int> temp = deal_thru(num1, num2);
				set_msg_map[set_id[i]].insert(set_msg_map[set_id[i]].end(), temp.begin(), temp.end());
			}
			else if (j.find("ALL") != string::npos) {
				//如果出现all，使用-99999表示
				//cout << "Msg: 存在SET=ALL的集合，后续解析可能会出错\n";
				set_msg_map[set_id[i]].push_back(-99999);
			}
			else {
				bool contain_alpha = false;
				for (auto& k : j) {
					if (isalpha(k)) {
						contain_alpha = true;
						break;
					}
				}
				if (contain_alpha == true) {
					cout << "Warning: SET集合中存在无法识别的关键字，请检查\n";
					set_msg_map[set_id[i]].clear();
					break;
				}
				else {
					set_msg_map[set_id[i]].push_back(stoi(j));
				}
			}
		}
		if (set_msg_map[set_id[i]].empty()) {
			set_msg_map.erase(set_id[i]);
		}
	}
	int das = 1;
};

void ReadBDF_::read_nodes() 
{
	vector<vector<string>> dim2_list = { {"GRID"},
										{"CORD2R","CORD2C","CORD1R","CORD1C"} };

	vector<vector<vector<string>>> deal_package;
	division(deal_all_packages_out[0], dim2_list, deal_package);
	vector<vector<string>> deal_nodes_package = deal_package[0];
	vector<vector<string>> deal_coord_package = deal_package[1];
	vector<vector<variant<int, float, string>>> deal_nodes_package_1 = change_data_type(deal_nodes_package);
	vector<vector<variant<int, float, string>>> deal_coord_package_1 = change_data_type(deal_coord_package);
	// cord1c和 cord1r 一行可能存在两个坐标系，需拆开
	vector<vector<variant<int, float, string>>> temp_deal_coord;
	for (auto i : deal_coord_package_1) {
		auto name = GETS(i[0]);
		if ((name == "CORD1C" || name == "CORD1R") && i.size() >= 9) {
			temp_deal_coord.emplace_back();
			temp_deal_coord.back().push_back(i[0]);
			temp_deal_coord.back().push_back(i[1]);
			temp_deal_coord.back().push_back(i[2]);
			temp_deal_coord.back().push_back(i[3]);
			temp_deal_coord.back().push_back(i[4]);
			temp_deal_coord.emplace_back();
			temp_deal_coord.back().push_back(i[0]);
			temp_deal_coord.back().push_back(i[5]);
			temp_deal_coord.back().push_back(i[6]);
			temp_deal_coord.back().push_back(i[7]);
			temp_deal_coord.back().push_back(i[8]);
		}
		else {
			temp_deal_coord.push_back(i);
		}
	}
	deal_coord_package_1 = temp_deal_coord;

	//contain_local_coord_index储存带有局部坐标系的节点index，在后续做处理
	vector<int> contain_local_coord_index;
	for (int i = 0; i < deal_nodes_package_1.size(); ++i) {
		auto* item = get_if<int>(&deal_nodes_package_1[i][1]);
		nodes_sequence.push_back(*item);
		if (holds_alternative<string>(deal_nodes_package_1[i][2])) { //如果是字符串，则代表没有坐标系
			vector<float> coord;
			for (int j = 0; j < 3; ++j) {
				coord.emplace_back(GETF(deal_nodes_package_1[i][3 + j]));
			}
			nodes_coord.emplace_back(coord);
		}
		else {
			contain_local_coord_index.push_back(i);
			nodes_coord.emplace_back();
		}
		if (deal_nodes_package_1[i].size() >= 7) {
			auto temp = get_if<int>(&deal_nodes_package_1[i][6]);
			if (temp) {
				if (*temp != -1)
				{
					local_sys_for_spc["node_id"].push_back(GETI(deal_nodes_package_1[i][1]));
					local_sys_for_spc["sys_id"].push_back(GETI(deal_nodes_package_1[i][6]));
				}
				else
				{
					fluid_nodes.insert(*item);
				}
			}

		}
	}
	for (auto& i : contain_local_coord_index) {
		auto origin_data = deal_nodes_package_1[i];
		vector<vector<variant<int, float, string>>> coord_sys = GeneralMethod::search_items(origin_data[2], deal_coord_package_1, 1, 1);
		vector<vector<float>> coord_sys_data = { {},
												{},
												{} };
		if (GETS(coord_sys[0][0]) == "CORD2R" || GETS(coord_sys[0][0]) == "CORD2R*") {
			int temp = 3;
			for (int j = 0; j < coord_sys_data.size(); ++j) {
				for (int k = 0; k < 3; ++k) {
					auto temp2 = GETF(coord_sys[0][temp]);
					coord_sys_data[j].push_back(temp2);
					temp++;
				}
			}
			vector<float> local_coord;
			for (int j = 3; j < 6; ++j) {
				local_coord.push_back(GETF(origin_data[j]));
			}
			vector<float> global_coord = from_local_coord_sys_to_global_coord_sys(local_coord, coord_sys_data);
			nodes_coord[i] = global_coord;
		}
		else if (GETS(coord_sys[0][0]) == "CORD1R" || GETS(coord_sys[0][0]) == "CORD1R*") {
			int a_id = GETI(coord_sys[0][2]), b_id = GETI(coord_sys[0][3]), c_id = GETI(coord_sys[0][4]);
			vector<float> a_coord, b_coord, c_coord;
			for (int j = 0; j < nodes_sequence.size(); ++j) {
				if (nodes_sequence[j] == a_id) {
					a_coord = nodes_coord[j];
				}
				else if (nodes_sequence[j] == b_id) {
					b_coord = nodes_coord[j];
				}
				else if (nodes_sequence[j] == c_id) {
					c_coord = nodes_coord[j];
				}
				if (!a_coord.empty() && !b_coord.empty() && !c_coord.empty()) {
					break;
				}
			}
			coord_sys_data[0] = a_coord;
			coord_sys_data[1] = b_coord;
			coord_sys_data[2] = c_coord;

			vector<float> local_coord;
			for (int j = 3; j < 6; ++j) {
				local_coord.push_back(GETF(origin_data[j]));
			}
			vector<float> global_coord = from_local_coord_sys_to_global_coord_sys(local_coord, coord_sys_data);
			nodes_coord[i] = global_coord;
		}
		else if (GETS(coord_sys[0][0]) == "CORD2C" || GETS(coord_sys[0][0]) == "CORD2C*") {
			int temp = 3;
			for (int j = 0; j < coord_sys_data.size(); ++j) {
				for (int k = 0; k < 3; ++k) {
					auto temp2 = GETF(coord_sys[0][temp]);
					coord_sys_data[j].push_back(temp2);
					temp++;
				}
			}

			//将柱坐标系下的点坐标转换为直角坐标系下坐标，再转换为全局坐标系下坐标。2024年5月28日15:42:30待测试，可能有错
			//2024年5月29日09:55:02 测试后发现origin_data[4]实际为角度，需要转换为弧度再参与计算
			vector<float> local_coord;
			local_coord.push_back(GETF(origin_data[3]) * cos(GETF(origin_data[4]) * M_PI / 180));
			local_coord.push_back(GETF(origin_data[3]) * sin(GETF(origin_data[4]) * M_PI / 180));
			/*local_coord.push_back(GETF(origin_data[3]));
			local_coord.push_back(GETF(origin_data[4]));*/
			local_coord.push_back(GETF(origin_data[5]));
			vector<float> global_coord = from_local_coord_sys_to_global_coord_sys(local_coord, coord_sys_data);
			nodes_coord[i] = global_coord;
		}
		else if (GETS(coord_sys[0][0]) == "CORD1C" || GETS(coord_sys[0][0]) == "CORD1C*") {
			int a_id = GETI(coord_sys[0][2]), b_id = GETI(coord_sys[0][3]), c_id = GETI(coord_sys[0][4]);
			vector<float> a_coord, b_coord, c_coord;
			for (int j = 0; j < nodes_sequence.size(); ++j) {
				if (nodes_sequence[j] == a_id) {
					a_coord = nodes_coord[j];
				}
				else if (nodes_sequence[j] == b_id) {
					b_coord = nodes_coord[j];
				}
				else if (nodes_sequence[j] == c_id) {
					c_coord = nodes_coord[j];
				}
				if (!a_coord.empty() && !b_coord.empty() && !c_coord.empty()) {
					break;
				}
			}
			coord_sys_data[0] = a_coord;
			coord_sys_data[1] = b_coord;
			coord_sys_data[2] = c_coord;

			vector<float> local_coord;
			local_coord.push_back(GETF(origin_data[3]) * cos(GETF(origin_data[4]) * M_PI / 180));
			local_coord.push_back(GETF(origin_data[3]) * sin(GETF(origin_data[4]) * M_PI / 180));
			/*local_coord.push_back(GETF(origin_data[3]));
			local_coord.push_back(GETF(origin_data[4]));*/
			local_coord.push_back(GETF(origin_data[5]));
			vector<float> global_coord = from_local_coord_sys_to_global_coord_sys(local_coord, coord_sys_data);
			nodes_coord[i] = global_coord;
		}
	}


	//    将所有id编号连续化
	int num = 0;
	for (auto i : nodes_sequence) {
		nodes_mapping_dict[i] = num;
		num++;
	}
	cout << "----> 节点数量: " << nodes_mapping_dict.size() << "\n";
	nodes_sequence = adjust_ids_vector(nodes_sequence, nodes_mapping_dict);
	set<int> node_type_temp;
	for (auto& i : fluid_nodes)
		node_type_temp.insert(nodes_mapping_dict[i]);
	fluid_nodes = node_type_temp; node_type_temp.clear();
}

void ReadBDF_::read_elements() {
    map<string, int> cout_control = {};
    vector<string> element_1d = { "CBEAM", "CBAR", "CGAP", };
    vector<string> element_conrod = { "CONROD" };
    vector<string> plane_element = { "CSHEAR", "CTRIA3", "CTRIA6", "CQUAD4", "CQUAD8" };
    vector<string> element_one_mapping_more = { "CHEXA", "CPENTA", "CTETRA" };

    vector<vector<string>>& deal_elements_package = deal_all_packages_out[1];
    for (auto& i : deal_elements_package) {
        if (i[0].find('*') != string::npos) {
            i[0].erase(i[0].find('*'), 1);
        }
    }

    vector<string> temp_ele_type_1, temp_ele_type_2, temp_ele_type_3, temp_ele_type_4, temp_ele_type_5;
    for (auto& i : nastran_type) {
        if (i != "CONM2") {
            temp_ele_type_1.push_back(i);
        }
    }
    for (auto& i : temp_ele_type_1) {
        if (std::find(element_1d.begin(), element_1d.end(), i) == element_1d.end()) {
            temp_ele_type_2.push_back(i);
        }
    }
    for (auto& i : temp_ele_type_2) {
        if (std::find(element_conrod.begin(), element_conrod.end(), i) == element_conrod.end()) {
            temp_ele_type_3.push_back(i);
        }
    }
    for (auto& i : temp_ele_type_3) {
        if (std::find(element_one_mapping_more.begin(), element_one_mapping_more.end(), i) == element_one_mapping_more.end()) {
            temp_ele_type_4.push_back(i);
        }
    }
    for (auto& i : temp_ele_type_4) {
        if (i != "CELAS1" && i != "CELAS2") {
            temp_ele_type_5.push_back(i);
        }
    }

    vector<vector<string>> dim2_list = { {"CONM2"}, element_1d, element_conrod, element_one_mapping_more, {"CELAS1", "CELAS2"},temp_ele_type_5 };
    vector<vector<vector<string>>> deal_elements_package_2;
    division(deal_elements_package, dim2_list, deal_elements_package_2);
    vector<vector<variant<int, float, string>>> deal_conm2_package, deal_1d_elements_package, deal_conrod_elements_package, deal_one_mapping_more_element_package, deal_celas_elements_packages, deal_normal_elements_packages;
    deal_conm2_package = change_data_type(deal_elements_package_2[0]);
    deal_1d_elements_package = change_data_type(deal_elements_package_2[1]);
    deal_conrod_elements_package = change_data_type(deal_elements_package_2[2]);
    deal_one_mapping_more_element_package = change_data_type(deal_elements_package_2[3]);
    deal_celas_elements_packages = change_data_type(deal_elements_package_2[4]);
    deal_normal_elements_packages = change_data_type(deal_elements_package_2[5]);

    vector<vector<string>> LABEL_1 = { {} }; //储存element_name

    //处理通常单元
    for (int i = 0; i < deal_normal_elements_packages.size(); ++i) {
        auto name = GETS(deal_normal_elements_packages[i][0]);
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        if ("CELAS1" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            auto i3 = GETI(deal_normal_elements_packages[i][3]);
            auto i5 = GETI(deal_normal_elements_packages[i][5]);
            elements_points[eid] = { i3, i5 };
        }
        else if ("CBUSH" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            auto i3 = GETI(deal_normal_elements_packages[i][3]);
            auto i4 = GETI(deal_normal_elements_packages[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else if ("CQUAD4" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            elements_points[eid] = {};
            for (int j = 3; j < 7; ++j) {
                elements_points[eid].push_back(GETI(deal_normal_elements_packages[i][j]));
            }
        }
        else if ("CQUAD8" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            elements_points[eid] = {};
            for (int j = 3; j < 11; ++j) {
                elements_points[eid].push_back(GETI(deal_normal_elements_packages[i][j]));
            }
        }
        else if ("CTRIA3" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            elements_points[eid] = {};
            for (int j = 3; j < 6; ++j) {
                elements_points[eid].push_back(GETI(deal_normal_elements_packages[i][j]));
            }
        }
        else if ("CTRIA6" == name) {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            elements_points[eid] = {};
            for (int j = 3; j < 9; ++j) {
                elements_points[eid].push_back(GETI(deal_normal_elements_packages[i][j]));
            }
        }
        else {
            auto eid = GETI(deal_normal_elements_packages[i][1]);
            elements_points[eid] = {};
            for (int j = 3; j < deal_normal_elements_packages[i].size(); ++j) {
                elements_points[eid].push_back(GETI(deal_normal_elements_packages[i][j]));
            }
        }
        elements_sequence[name].push_back(GETI(deal_normal_elements_packages[i][1]));
        auto temp_for_next_line = static_cast<double>(GETI(deal_normal_elements_packages[i][2]));
    }

    //    处理conm2单元
    for (int i = 0; i < deal_conm2_package.size(); ++i) {
        string name = GETS(deal_conm2_package[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        auto eid = GETI(deal_conm2_package[i][1]);
        auto pid = GETI(deal_conm2_package[i][2]);
        auto mass_value = static_cast<double>(GETF(deal_conm2_package[i][4]));
        //if (! GeneralMethod::string_is_in_vector(elements_names,name)){
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CONM2") {
            elements_points[eid] = { pid };
        }

        elements_sequence[name].push_back(eid);
    }


    //    处理1d单元
    vector<vector<variant<int, float, string>>> deal_1d_elements_with_direction, deal_1d_elements_with_coord, deal_1d_elements_with_onenode;
    for (auto i : deal_1d_elements_package) {
        if (GeneralMethod::isVariantEmpty(i[5]) && GeneralMethod::isVariantEmpty(i[6]) && GeneralMethod::isVariantEmpty(i[7])) {
            deal_1d_elements_with_coord.push_back(i);
        }
        else if (holds_alternative<float>(i[5]) && holds_alternative<float>(i[6]) && holds_alternative<float>(i[7])) {
            deal_1d_elements_with_direction.push_back(i);
        }
        else if (holds_alternative<int>(i[5]))
        {
            deal_1d_elements_with_onenode.push_back(i);
        }
    }
    //    处理含有矢量方向的1d单元
    for (int i = 0; i < deal_1d_elements_with_direction.size(); ++i) {
        string name = GETS(deal_1d_elements_with_direction[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        auto eid = GETI(deal_1d_elements_with_direction[i][1]);
        auto pid = GETI(deal_1d_elements_with_direction[i][2]);
        auto i5 = GETF(deal_1d_elements_with_direction[i][5]);
        auto i6 = GETF(deal_1d_elements_with_direction[i][6]);
        auto i7 = GETF(deal_1d_elements_with_direction[i][7]);
        //if (! GeneralMethod::string_is_in_vector(elements_names,name)){
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CBAR") {
            auto i3 = GETI(deal_1d_elements_with_direction[i][3]);
            auto i4 = GETI(deal_1d_elements_with_direction[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else if (name == "CBEAM") {
            auto i3 = GETI(deal_1d_elements_with_direction[i][3]);
            auto i4 = GETI(deal_1d_elements_with_direction[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else if (name == "CGAP") {
            auto i3 = GETI(deal_1d_elements_with_direction[i][3]);
            auto i4 = GETI(deal_1d_elements_with_direction[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else {
            elements_points[eid] = {};
            for (int j = 3; j < deal_1d_elements_with_direction[i].size(); ++j) {
                elements_points[eid].push_back(GETI(deal_1d_elements_with_direction[i][j]));
            }
        }
        elements_sequence[name].push_back(eid);
        auto pid_float = static_cast<double>(pid);

    }

    //    处理包含局部坐标系的1d单元
    for (int i = 0; i < deal_1d_elements_with_coord.size(); ++i) {
        string name = GETS(deal_1d_elements_with_coord[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        auto eid = GETI(deal_1d_elements_with_coord[i][1]);
        auto pid = GETI(deal_1d_elements_with_coord[i][2]);
        auto coord = GETI(deal_1d_elements_with_coord[i][8]);
        //if (! GeneralMethod::string_is_in_vector(elements_names,name)){
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CBAR") {
            auto i3 = GETI(deal_1d_elements_with_coord[i][3]);
            auto i4 = GETI(deal_1d_elements_with_coord[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else if (name == "CBEAM") {
            auto i3 = GETI(deal_1d_elements_with_coord[i][3]);
            auto i4 = GETI(deal_1d_elements_with_coord[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else if (name == "CGAP") {
            auto i3 = GETI(deal_1d_elements_with_coord[i][3]);
            auto i4 = GETI(deal_1d_elements_with_coord[i][4]);
            elements_points[eid] = { i3, i4 };
        }
        else {
            elements_points[eid] = {};
            for (int j = 3; j < deal_1d_elements_with_coord[i].size(); ++j) {
                elements_points[eid].push_back(GETI(deal_1d_elements_with_coord[i][j]));
            }
        }
        elements_sequence[name].push_back(eid);

    }

    //  处理用节点ID代替矢量的1d单元
    for (int i = 0; i < deal_1d_elements_with_onenode.size(); ++i)
    {
        string name = GETS(deal_1d_elements_with_onenode[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        int eid = GETI(deal_1d_elements_with_onenode[i][1]);
        int pid = GETI(deal_1d_elements_with_onenode[i][2]);
        int i3 = GETI(deal_1d_elements_with_onenode[i][3]);
        int i4 = GETI(deal_1d_elements_with_onenode[i][4]);
        int i5 = GETI(deal_1d_elements_with_onenode[i][5]);
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CBAR" || name == "CBEAM" || name == "CGAP") {
            elements_points[eid] = { i3, i4 };
        }
        elements_sequence[name].push_back(eid);
        vector<float> GA_coord = nodes_coord[nodes_mapping_dict[i3]];
        vector<float> G0_coord = nodes_coord[nodes_mapping_dict[i5]];

        vector<float> _vector = GeneralMethod::minus_vector(G0_coord, GA_coord);
        auto pid_double = static_cast<double>(pid);

    }

    //    处理conrod单元
    for (int i = 0; i < deal_conrod_elements_package.size(); ++i) {
        string name = GETS(deal_1d_elements_with_coord[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        auto eid = GETI(deal_1d_elements_with_coord[i][1]);
        auto area = static_cast<double>(GETF(deal_1d_elements_with_coord[i][5]));
        //if (! GeneralMethod::string_is_in_vector(elements_names,name)){
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CONROD") {
            auto i2 = GETI(deal_conrod_elements_package[i][2]);
            auto i3 = GETI(deal_conrod_elements_package[i][3]);
            elements_points[eid] = { i2, i3 };
        }
        elements_sequence[name].push_back(eid);

    }

    //    处理一对多单元
    for (int i = 0; i < deal_one_mapping_more_element_package.size(); ++i) {
        string name = GETS(deal_one_mapping_more_element_package[i][0]);
        if (cout_control.count(name) == 0) {
            cout << "----> Converting " << name << "...\n";
            cout_control[name] = 1;
        }
        auto eid = GETI(deal_one_mapping_more_element_package[i][1]);
        auto pid = GETI(deal_one_mapping_more_element_package[i][2]);
        if (name == "CHEXA" && deal_one_mapping_more_element_package[i].size() - 3 >= 20) {
            name = "CHEXA20";
        }
        else if (name == "CPENTA" && deal_one_mapping_more_element_package[i].size() - 3 >= 15) {
            name = "CPENTA15";
        }
        else if (name == "CTETRA" && deal_one_mapping_more_element_package[i].size() - 3 >= 10) {
            name = "CTETRA10";
        }
        //if (! GeneralMethod::string_is_in_vector(elements_names,name)){
        if (std::find(elements_names.begin(), elements_names.end(), name) == elements_names.end()) {
            elements_names.push_back(name);
            elements_sequence[name] = {};
        }
        if (name == "CHEXA20") { // 2024年1月29日16:33:38 nastran 和 simdroid 六面体20节点单元 节点顺序不一样，需要额外处理
            vector<int> temp_index;
            for (int j = 3; j < deal_one_mapping_more_element_package[i].size(); ++j) {
                if (holds_alternative<int>(deal_one_mapping_more_element_package[i][j])) {
                    temp_index.push_back(GETI(deal_one_mapping_more_element_package[i][j]));
                }
            }
            // 使用swap函数
            std::swap(temp_index[13], temp_index[17]);
            std::swap(temp_index[12], temp_index[16]);
            std::swap(temp_index[19], temp_index[15]);
            std::swap(temp_index[14], temp_index[18]);
            elements_points[eid] = temp_index;
        }
        else if (name == "CPENTA15") { // 22024年1月30日10:19:47 nastran 和 simdroid 五面体15节点单元 节点顺序不一样，需要额外处理
            vector<int> temp_index;
            for (int j = 3; j < deal_one_mapping_more_element_package[i].size(); ++j) {
                if (holds_alternative<int>(deal_one_mapping_more_element_package[i][j])) {
                    temp_index.push_back(GETI(deal_one_mapping_more_element_package[i][j]));
                }
            }
            // 使用swap函数
            std::swap(temp_index[9], temp_index[12]);
            std::swap(temp_index[10], temp_index[13]);
            std::swap(temp_index[11], temp_index[14]);
            elements_points[eid] = temp_index;
        }
        else {
            for (int j = 3; j < deal_one_mapping_more_element_package[i].size(); ++j) {
                if (holds_alternative<int>(deal_one_mapping_more_element_package[i][j])) {
                    elements_points[eid].push_back(GETI(deal_one_mapping_more_element_package[i][j]));
                }
            }
        }
        elements_sequence[name].push_back(eid);
        int length = deal_one_mapping_more_element_package[i].size() - 3;
    }

    //    处理不连续编号
    int id_num = 0;
    vector<int> all_ele_ids = {};
    for (auto i : elements_sequence) {
        all_ele_ids.insert(all_ele_ids.end(), i.second.begin(), i.second.end());
    }
    sort(all_ele_ids.begin(), all_ele_ids.end());
    for (auto i : all_ele_ids) {
        ele_mapping_dict[i] = id_num;
        id_num++;
    }
    for (auto i : elements_sequence) {
        elements_sequence[i.first] = adjust_ids_vector(i.second, ele_mapping_dict);
    }
    map<int, vector<int>> temp_map = {};
    for (auto& i : elements_points) {
        int ele_id = i.first;
        auto k = adjust_id(ele_id, ele_mapping_dict);
        auto v = adjust_ids_vector(i.second, nodes_mapping_dict);
        temp_map[k] = v;
    }
    elements_points = temp_map;

    //处理流体单元
    //todo

    cout << "----> 单元数量: " << ele_mapping_dict.size() << "\n";
}



void ReadBDF_::look_for_msg(vector<string>& all_packages) {
	string blank8 = "        ";
	string plus = "+";
	string star = "*";
	string line = "";

	fstream file(read_file_path, ios::in);
	if (!file.is_open()) {
		cout << "Warning: path " << read_file_path << " 打开文件失败\n";
		return;
	}

	clock_t start, end;
	start = clock();
	cout << "Start reading file...\n";
	file.seekg(0, std::ios::end);//2024年1月26日14:48:28新的读取方式。
	std::streampos fileSize = file.tellg();
	file.seekg(0, std::ios::beg);
	std::string filedata;
	filedata.reserve(fileSize);
	filedata.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
	file.close();
	end = clock();
	cout << "----> Read file to cache finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";
	start = clock();
	stringstream ss(filedata);
	char split_ = '\n';
	vector<string> file_data_list;
	while (getline(ss, line, split_)) {
		file_data_list.push_back(line);
	}
	end = clock();
	cout << "----> Split the file data finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";

	int keyword_length = all_keywords.size();
	for (int i = 0; i < keyword_length; ++i) {
		all_keywords.push_back(all_keywords[i] + "*");
	}
	map<string, int> temp_keyword; // 把keyword转换为8长度，以便后续判断。放入map的key中，加快处理速度。
	for (auto& i : all_keywords) {
		temp_keyword[i] = 0;
		int blank_num = 8 - i.size();
		while (blank_num>0)
		{
			i.push_back(' ');
			blank_num -- ;
		}
		temp_keyword[i] = 0;
	}

	start = clock();// 2024年1月29日11:22:55测试新的筛选方式 加快处理速度
	pair<string, int> control;
	string last_key = "a_dont_be_used_str";
	bool under_begin_bulk = false;
	for (auto& i : file_data_list) {
		if (i.find("BEGIN BULK") != string::npos || i.find("begin bulk") != string::npos) {
			under_begin_bulk = true;
		}
		string temp_line = i.substr(0, 8);
		if (temp_line.find(",") != string::npos) {
			temp_line = temp_line.substr(0, temp_line.find_first_of(","));
		}
		if (temp_keyword.count(temp_line) != 0) {
			
			std::transform(i.begin(), i.end(), i.begin(), ::toupper);
			all_packages.push_back(i);
			control = make_pair(temp_line, 1);
			last_key = temp_line;
		}
		else {
			if (temp_line != "        " && temp_line.find("*") == string::npos && temp_line.find("+") == string::npos && temp_line.find("/") == string::npos && temp_line.find("\\") == string::npos && temp_line != "") {
				last_key = "a_dont_be_used_str";
				control = make_pair(temp_line, 1);
			}
			if (temp_keyword.count(last_key) != 0) {
				std::transform(i.begin(), i.end(), i.begin(), ::toupper);
				if (i.find(blank8) == 0 && control.first == last_key && control.second == 1) {
					all_packages.push_back(i);
				}
				else if (i.find(star) == 0 && control.first == last_key && control.second == 1) {
					all_packages.push_back(i);
				}
				else if (i.find(plus) == 0 && control.first == last_key && control.second == 1) {
					all_packages.push_back(i);
				}
				else if (i.find(",") == 0 && control.first == last_key && control.second == 1) {
					all_packages.push_back("+" + i);
				}
			}
		}
	}

	end = clock();
	cout << "----> Filter data by keywords finished in " << double(end - start) / CLOCKS_PER_SEC << "s\n";

}


void ReadBDF_::split_and_combine(vector<string> dim1_list, vector<vector<string>>& deal_all_packages) {
	//保留''并且无多余空格的剪切
	auto cut_line_5 = [](string& line, vector<int>& len_list)-> vector<string>
	{
		int sta = 0, end = 0, control = 0;
		vector<string> result = {}, result_1 = {};

		//新增 如果字符串中有逗号，按照逗号进行分割
		if (line.find(",") != string::npos) {
			stringstream ss(line);
			string temp_data;
			char split_ = ',';
			while (getline(ss, temp_data, split_)) {
				if (temp_data.find("+") != string::npos) {
					result_1.push_back("");
				}
				else {
					result_1.push_back(temp_data);
				}
			}
		}
		else {
			for (int i : len_list) {
				sta += end;
				end = i;
				if (sta + end >= line.size()) {
					control = 1;
				}
				string temp = line.substr(sta, end);
				result_1.push_back(temp);
				if (control == 1) {
					break;
				}
			}
		}
		for (string& i : result_1) {
			GeneralMethod::strip(i);
			result.push_back(i);
		}
		return result;
	};
	
	//保留空格的剪切
	auto cut_line_4 = [](string& line, vector<int>& len_list)-> vector<string> {
		int sta = 0, end = 0;
		vector<string> result = {}, result_1 = {};
		if (line.find(",") != string::npos) {
			stringstream ss(line);
			string temp_data;
			char split_ = ',';
			while (getline(ss, temp_data, split_)) {
				if (temp_data.find("+") != string::npos) {
					result_1.push_back("");
				}
				else {
					result_1.push_back(temp_data);
				}
			}
		}
		else {
			for (int i : len_list) {
				sta += end;
				end = i;
				string temp = line.substr(sta, end);
				result_1.push_back(temp);
				if (sta + end >= line.size()) {
					break;
				}
			}
		}
		for (string& i : result_1) {
			GeneralMethod::strip(i);
			result.push_back(i);
		}
		return result;
	};


	string blank8 = "        ", star = "*       ", plus = "+";
	for (int i = 0; i < dim1_list.size(); ++i) {
		string& line = dim1_list[i];

		if (line.substr(0, 8) != blank8 && line.substr(0, 1) != plus) {  //2024年2月1日15:00:45优化测试
			if (line.find('*') == string::npos) {
				vector<int> len_list = { 8, 8, 8, 8, 8, 8, 8, 8, 8 };
				vector<string> line_deal = cut_line_5(line, len_list);
				deal_all_packages.push_back(line_deal);
			}
			else {
				if (line.substr(0, 8) == star || line.find("*") == 0) {
					vector<int> len_list = { 16, 16, 16, 16 };
					//string line_1 = line.substr(8);
					string line_1;
					if (line.find(",") != string::npos) {
						line_1 = line.substr(line.find_first_of(",") + 1);
					}
					else {
						line_1 = line.substr(8);
					}
					vector<string> line_deal = cut_line_5(line_1, len_list);
					if (line_deal.size() < 4) {
						line_deal = cut_line_4(line_1, len_list);
					}
					deal_all_packages.back().insert(deal_all_packages.back().end(), line_deal.begin(), line_deal.end());
				}
				else {
					vector<int> len_list = { 8, 16, 16, 16, 16 };
					vector<string> line_deal = cut_line_5(line, len_list);
					if (line_deal.size() < 5) {
						line_deal = cut_line_4(line, len_list);
					}
					deal_all_packages.push_back(line_deal);
				}
			}
		}
		else {
			vector<int> len_list = { 8, 8, 8, 8, 8, 8, 8, 8 };
			string line_1;
			if (line.find(",") != string::npos) {
				line_1 = line.substr(line.find_first_of(",") + 1);
			}
			else {
				line_1 = line.substr(8);
			}
			vector<string> line_deal = cut_line_5(line_1, len_list);
			deal_all_packages.back().insert(deal_all_packages.back().end(), line_deal.begin(), line_deal.end());
		}
	}
	
}

void ReadBDF_::division(vector<vector<string>>& origin_data, vector<vector<string>>& dim2_list, vector<vector<vector<string>>>& result) {

	vector<vector<string>> dim2_list_copy = dim2_list;
	for (int i = 0; i < dim2_list_copy.size(); ++i) {
		for (int j = 0; j < dim2_list_copy[i].size(); ++j) {
			dim2_list[i].push_back(dim2_list[i][j] + "*");
		}
	}
	vector<map<string, int>> temp_dim2_list;//2024年1月26日15:54:05按照map进行处理，加快处理速度
	for (auto& i : dim2_list) {
		temp_dim2_list.emplace_back();
		for (auto& j : i) {
			temp_dim2_list.back()[j] = 1;
		}
	}
	for (int i = 0; i < dim2_list.size(); ++i) {
		result.emplace_back();
	}
	for (vector<string> i : origin_data) {
		for (int j = 0; j < temp_dim2_list.size(); ++j) {
			if (temp_dim2_list[j].count(i[0]) != 0) {
				result[j].push_back(i);
			}
		}
	}
}

vector<int> ReadBDF_::deal_thru(int node1, int  node2) {
	vector<int> result = {};
	for (int i = node1; i <= node2; ++i) {
		result.push_back(i);
	}
	return result;
}

vector<vector<variant<int, float, string>>> ReadBDF_::change_data_type(vector<vector<string >>& origin_data) {
	vector<vector<variant<int, float, string>>> result;
	string j_deal;
	for (vector<string> i : origin_data) {
		result.emplace_back();
		for (string j : i) {
			if (j.find('.') != string::npos) {
				if (GeneralMethod::is_contain_alpha_except_e(j))
				{
					result.back().emplace_back(j);
					continue;
				}
				if (j.find('e') == string::npos && j.find('E') == string::npos) {
					if (j.find('+') != string::npos) {
						int index = j.find('+');
						string part1 = j.substr(0, index);
						string part2 = j.substr(index + 1, j.size());
						j_deal = part1.append("e+").append(part2);
					}
					else if (j.find('-') != string::npos) {
						int index = j.rfind('-');
						if (index != 0) {
							string part1 = j.substr(0, index);
							string part2 = j.substr(index + 1, j.size());
							if (stof(part2) > 37) {
								part2 = "37";
							}
							j_deal = part1.append("e-").append(part2);
						}
						else {
							j_deal = j;
						}
					}
					else {
						j_deal = j;
					}
				}
				else {
					for (char& c : j) {
						c = std::toupper(c);
					}
					auto index = j.find_last_of("E");
					string part1 = j.substr(0, index);
					string part2 = j.substr(index + 1);
					if (stoi(part2) > 37 || stoi(part2) < -37)
					{
						j_deal = "0";
					}
					else
					{
						j_deal = j;
					}
				}
				result.back().emplace_back(stof(j_deal));
			}
			else {

				bool check = true;
				if (j.empty()) {
					check = false;
				}
				for (char k : j) {
					if (isdigit(k) || k == '-' || k == '+') {
						continue;
					}
					else {
						check = false;
						break;
					}
				}
				if (check) {
					result.back().emplace_back(stoi(j));
				}
				else {
					result.back().emplace_back(j);
				}
			}
		}
	}
	return result;
};

vector<float> ReadBDF_::from_local_coord_sys_to_global_coord_sys(vector<float>& local_coord, vector<vector<float>>& local_coord_sys) {
	vector<float> result = local_coord_sys[0];

	vector<float> coord_sys_a = local_coord_sys[0];  //局部坐标系原点
	vector<float> coord_sys_b = local_coord_sys[1];  //局部坐标系z轴上一点
	vector<float> coord_sys_c = local_coord_sys[2];  //局部坐标系xz平面上一点
	vector<float> vector_ac;
	for (int i = 0; i < coord_sys_c.size(); ++i) {
		vector_ac.push_back(coord_sys_c[i] - coord_sys_a[i]);
	}

	vector<float> coord_sys_z;
	vector<float> coord_sys_y;
	vector<float> coord_sys_x;
	for (int i = 0; i < coord_sys_b.size(); ++i) {
		coord_sys_z.push_back(coord_sys_b[i] - coord_sys_a[i]);
	}
	coord_sys_y = GeneralMethod::matrix_cross_multiply(coord_sys_z, vector_ac);
	coord_sys_x = GeneralMethod::matrix_cross_multiply(coord_sys_y, coord_sys_z);

	float vector_x_length;
	float vector_y_length;
	float vector_z_length;
	vector_x_length = pow(pow(coord_sys_x[0], 2) + pow(coord_sys_x[1], 2) + pow(coord_sys_x[2], 2), 0.5);
	vector_y_length = pow(pow(coord_sys_y[0], 2) + pow(coord_sys_y[1], 2) + pow(coord_sys_y[2], 2), 0.5);
	vector_z_length = pow(pow(coord_sys_z[0], 2) + pow(coord_sys_z[1], 2) + pow(coord_sys_z[2], 2), 0.5);

	float ratio_x;
	float ratio_y;
	float ratio_z;
	ratio_x = local_coord[0] / vector_x_length;
	ratio_y = local_coord[1] / vector_y_length;
	ratio_z = local_coord[2] / vector_z_length;

	vector<float> along_x_vector;
	vector<float> along_y_vector;
	vector<float> along_z_vector;
	for (float i : coord_sys_x) {
		along_x_vector.push_back(i * ratio_x);
	}
	for (float i : coord_sys_y) {
		along_y_vector.push_back(i * ratio_y);
	}
	for (float i : coord_sys_z) {
		along_z_vector.push_back(i * ratio_z);
	}
	result = GeneralMethod::add_vector(result, along_x_vector);
	result = GeneralMethod::add_vector(result, along_y_vector);
	result = GeneralMethod::add_vector(result, along_z_vector);
	return result;
}

//按照mapping 调整id
vector<int> ReadBDF_::adjust_ids_vector(vector<int>& origin_data, map<int, int>& mapping_dict) {
	vector<int> result;
	for (auto i : origin_data) {
		if (mapping_dict.find(i) != mapping_dict.end())
		{
			result.push_back(mapping_dict[i]);
		}
	}
	return result;
};
int ReadBDF_::adjust_id(int& origin_data, map<int, int>& mapping_dict) {

	if (mapping_dict.find(origin_data) != mapping_dict.end())
	{
		return mapping_dict[origin_data];
	}
	return 0;
}